<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use qiniu\Qn;
class Goods extends Base{

    //前端获取数据列表
    public function getList(){
        $param = input('');

        $search_type = $param['type'] != '0' ? ['type' => $param['type']]:[];//判断是否需要筛选 分类

        $this->info['data'] = Db::table('an_goods')
        ->field(['id','type','productName','productImage','productImageList','productPrice','sort'])
        ->where('a_show','1')
        ->where($search_type)
        ->order('sort desc')
        ->page($param['page'],10)
        ->select();
        
        // $this->info['total'] = Db::query("select count(*)total from an_goods where a_show='1'")[0]['total'];
        $this->info['total'] = Db::table('an_goods')
        ->where('a_show','1')
        ->where($search_type)
        ->count();
        return json($this->info);
    } 
    
    public function get_help(){
        $this->info['data'] = '<p><strong>温馨提示:</strong></p><p><strong><br></strong></p><p>欢迎光临安之小卖部，本平台由本人自主开发，所有商品由本人上传，请放心使用</p><br><p>项目刚上线不久，产品正在陆续添加中</p><p><br></p><p>本平台支持直接进行在线下单</p><p><br></p><p>或者 直接微信联系我 ，微信：131 2813 8787</p><p><br></p><p>先付款，后采购</p><p>平台上面所有的价格，以当天代购价格为准</p><p>如果当天价格发生变化，会提前联系您还是否需要代购</p><p><br></p><p>所有产品不包邮，深圳发货，可以到付，可以选择我这里付，邮费寄多少算多少</p><p><br></p><p>【 保证正品，亲自采购 】</p>';
        return json($this->info);
    } 

    //关键词搜索
    public function get_search_list($type){
        $info = db('goods')
        ->field('productName,id,productPrice,productImageList,productImage')
        ->where('productName','like','%'.$type.'%')
        ->where('a_show','1')
        ->order('id desc')
        ->select();
        if(!empty($info[0])){
            $this->info['code']=200;
            $this->info['data']=$info;
        }else{
            $this->info['code']=400;
            $this->info['tips']=$type;
        }
        return json($this->info);
    }    


    //前端获取详细内容
    public function get_one_detail_m($id){
        $info = Db::table('an_goods')->where('id',$id)->select();
        $info['productPrice'] = '价格待定';
        $this->info['data'] = $info;
        return json($this->info);
    }  

    //提交订单
    // public function ajax_add_order_a(){
    //     $data = input('');

    //     $user['name'] = $data['name'];
    //     $user['phone'] = $data['phone'];
    //     $user['addr'] = $data['addr'];
    //     $user['admin_id'] = $data['admin_id'];
    //     $user['orderID'] = date('YmdHis',time());
    //     $user['created_time'] = date('y-m-d h:i:s',time());        
    //     $bool = db('goods_user')->insert($user);

    //     for($i=0;$i<count($data['goods_list']);$i++){
    //         $order['productID'] = $data['goods_list'][$i]['id'];
    //         $order['productImage'] = $data['goods_list'][$i]['productImage'];
    //         $order['productName'] = $data['goods_list'][$i]['productName'];
    //         $order['productPrice'] = $data['goods_list'][$i]['productPrice'];
    //         $order['productNum'] = $data['goods_list'][$i]['sp_num'];
    //         $order['orderID'] = $user['orderID'];
    //         $bool = db('goods_order')->insert($order);
    //     }
    //     if($bool == 1){
    //         $this->info['code']=200;
    //         $this->info['tips']='添加成功';
    //     }else{
    //         $this->info['code']=400;
    //         $this->info['tips']='添加失败';
    //     }
    //     return json($this->info);
    // }    
}
